<?php
/**
 * Index file
 * @package waterwheel
 */
/*
 *  Copyright 2010 Andrew Rickmann
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 *
 * Finds the file the given class name and includes it ready for the class to be instantiated.
 * This is passed to the spl_autoload_register function to automate the process.
 *
 * @param String $classname The name of the class being instantiated.
 */
function application_autoload($classname){
	$path = APPLICATION_PATH . DIRECTORY_SEPARATOR . "classes" . DIRECTORY_SEPARATOR;
	$file = $path . $classname . '.php';
	if( file_exists($file) ){
		include_once($file);
	}
}

//setup the class autoloader
spl_autoload_register('application_autoload');


/**
 *
 * Handles exceptions
 * @todo review once request is setup.
 *
 * @param <type> $exception
 */
function application_exception_handler( $exception ){

    //for now keep it simple
    echo $exception->getMessage();

    //once the request is setup
    //we can call an error request
    $request = new request( array('error') );
    exit;


}

set_exception_handler('application_exception_handler');

$indexpath = pathinfo($_SERVER['PHP_SELF']);
$index_url = preg_replace('/^(?:\/|\\\\)(.*)\/?$/U' , "$1" , $indexpath['dirname']);

//if the index URL is not empty add a slash at the start
//if we just add it then double slashing will occur
if ( !empty($index_url) ){
	$index_url = '/' . $index_url;
}

//setup some constants
define('APPLICATION_PATH' , dirname(__FILE__) );
define('APPLICATION_URL' , 'http://' . $_SERVER['HTTP_HOST'] . $index_url );
define('THEMES_URL' , APPLICATION_URL . '/themes');
define('THEMES_PATH' , APPLICATION_PATH . '/themes');
define('VERSION' , 0.1);

//start a session
session_start();

//load the plugins
$plugins = new plugins();

//run the application
$request = new request();



?>